<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="128px"
        inline
      >
        <el-form-item label="身份证号" prop="param.idCard">
          <el-input
            style="width: 240px"
            v-model="queryParams.param.idCard"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="param.name">
          <el-input
            style="width: 240px"
            v-model="queryParams.param.name"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属项目" prop="param.projectId">
          <el-select
            style="width: 240px"
            clearable
            v-model="queryParams.param.projectId"
            placeholder="请选择所属项目"
          >
            <el-option v-for="item in projectList" :key="item.id" :label="item.topicShortName" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="住院号" prop="param.hospitalizationNumber">
          <el-input
            style="width: 240px"
            v-model="queryParams.param.hospitalizationNumber"
            placeholder="请输入住院号"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="住院医生名称" prop="param.doctorName">
          <el-input
            style="width: 240px"
            v-model="queryParams.param.doctorName"
            placeholder="请输入住院医生名称"
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="档案号" prop="param.fileNum">
          <el-input
            style="width: 240px"
            v-model="queryParams.param.fileNum"
            placeholder="请输入档案号"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="档案状态" prop="param.caseStatus">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.caseStatus"
            placeholder="请选择档案状态"
          >
            <el-option label="未提交" value="0"> </el-option>
            <el-option label="已提交" value="1"> </el-option>
            <el-option label="已审核" value="2"> </el-option>
            <el-option label="已驳回" value="3"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属科室" prop="param.departmentId">
          <el-input
            v-model="queryParams.param.departmentId"
            placeholder="请输入所属科室"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
          <!-- <el-select
            style="width: 240px"
            v-model="queryParams.param.departmentId"
            placeholder="请选择所属科室"
          >
            <el-option label="神经内科一" value="神经内科一"> </el-option>
            <el-option label="神经内科二" value="神经内科二"> </el-option>
            <el-option label="神经外科一" value="神经外科一"> </el-option>
            <el-option label="神经外科二" value="神经外科二"> </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="数据来源" prop="param.source">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.source"
            placeholder="请选择数据来源"
          >
            <el-option
              v-for="item in dict.type.data_source"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否上报" prop="param.reportStatus">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.reportStatus"
            placeholder="请选择是否上报"
          >
            <el-option label="是" value="1"> </el-option>
            <el-option label="否" value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="治疗类型" prop="param.treatmentTypes">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.treatmentTypes"
            placeholder="请选择治疗类型"
            multiple
            clearable
          >
            <el-option label="AIS静脉溶栓" value="AIS静脉溶栓"> </el-option>
            <el-option label="AIS介入再通" value="AIS介入再通"> </el-option>
            <el-option label="脑出血" value="脑出血"> </el-option>
            <el-option label="颅内动脉瘤" value="颅内动脉瘤"> </el-option>
            <el-option label="CES/CAS" value="CES/CAS"> </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item
          label="手术方式"
          v-if="queryParams.param.treatmentTypes"
          prop="param.operationWay"
        >
          <el-select
            style="width: 240px"
            v-model="queryParams.param.operationWay"
            placeholder="请选择手术方式"
            multiple
          >
            <el-option :disabled="judge('0')" label="溶栓" value="0">
            </el-option>
            <el-option :disabled="judge('1')" label="桥接" value="1">
            </el-option>
            <el-option :disabled="judge('2')" label="AIS介入再通" value="2">
            </el-option>
            <el-option :disabled="judge('3')" label="开颅血肿清除术" value="3">
            </el-option>
            <el-option
              :disabled="judge('4')"
              label="去骨瓣减压术脑室镜下血肿抽吸术"
              value="4"
            >
            </el-option>
            <el-option :disabled="judge('5')" label="钻孔血肿抽吸术" value="5">
            </el-option>
            <el-option :disabled="judge('6')" label="复合手术" value="6">
            </el-option>
            <el-option :disabled="judge('7')" label="其他" value="7">
            </el-option>
            <el-option :disabled="judge('8')" label="接入栓塞术" value="8">
            </el-option>
            <el-option :disabled="judge('9')" label="闭夹术" value="9">
            </el-option>
            <el-option :disabled="judge('10')" label="标准式CEA" value="10">
            </el-option>
            <el-option :disabled="judge('11')" label="外翻式CEA" value="11">
            </el-option>
            <el-option :disabled="judge('12')" label="CAS" value="12">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- 治疗类型为：AIS静脉溶栓 手术方式可选：溶栓、桥接
        治疗类型为：AIS血管内治疗 手术方式可选：AIS介入再通、桥接
        治疗类型为：脑出血手术 手术方式可选：开颅血肿清除术、去骨瓣减压术脑室镜下血肿抽吸术、钻孔血肿抽吸术、复合手术、其他
        治疗类型为：颅内动脉瘤手术 手术方式为：接入栓塞术、闭夹术、复合手术
        治疗类型为：CEA/CAS 手术方式为：标准式CEA、外翻式CEA、CAS、复合手术 -->
        <el-form-item label="手术操作标化名称" prop="param.operationName">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.operationName"
            placeholder="请选择手术操作标化名称"
            multiple
          >
            <el-option label="急性脑梗死介入再通" value="0"> </el-option>
            <el-option label="脑出血开颅血肿清除" value="1"> </el-option>
            <el-option label="脑出血穿刺引流" value="2"> </el-option>
            <el-option label="动脉瘤开颅" value="3"> </el-option>
            <el-option label="动脉瘤栓塞" value="4"> </el-option>
            <el-option label="颈动脉支架" value="5"> </el-option>
            <el-option label="颈动脉内膜剥脱" value="6"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出院日期" prop="param.leaveTime">
          <el-date-picker
            v-model="queryParams.param.leaveTime"
            type="daterange"
            align="right"
            unlink-panels
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            @click="downloadData"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" fixed />
        <el-table-column
          type="index"
          label="序号"
          width="55"
          align="center"
          fixed
        />
        <el-table-column prop="projectId" label="所属项目" width="200">
        </el-table-column>
        <el-table-column prop="fileNum" label="档案号" width="100">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="100">
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="100">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_user_sex"
              :value="scope.row.sex"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="hospitalizationNumber"
          label="住院号"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="doctorName" label="住院医师" width="100">
        </el-table-column>
        <el-table-column
          prop="riskLevel"
          label="治疗类型"
          align="center"
          width="500"
        >
          <template slot-scope="scope">
            <el-tag
            v-if="scope.row.recordVoList && scope.row.recordVoList.length"
              :effect="
                scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  }) ? scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  })
                  .answer.split(',')
                  .findIndex((item) => {
                    return item == 'AIS静脉溶栓';
                  }) != -1
                  ? 'dark'
                  : 'plain' : 'plain'
              "
            >
              AIS静脉溶栓
            </el-tag>
            <el-tag
              v-if="scope.row.recordVoList && scope.row.recordVoList.length"
              :effect="
                scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  }) ?  scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  })
                  .answer.split(',')
                  .findIndex((item) => {
                    return item == 'AIS介入再通';
                  }) != -1
                  ? 'dark'
                  : 'plain' : 'plain'
              "
            >
              AIS介入再通
            </el-tag>
            <el-tag
              v-if="scope.row.recordVoList && scope.row.recordVoList.length"
              :effect="
                scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  }) ? scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  })
                  .answer.split(',')
                  .findIndex((item) => {
                    return item == '脑出血';
                  }) != -1
                  ? 'dark'
                  : 'plain': 'plain'
              "
            >
              脑出血
            </el-tag>
            <el-tag
              v-if="scope.row.recordVoList && scope.row.recordVoList.length"
              :effect="
                scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  }) ? scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  })
                  .answer.split(',')
                  .findIndex((item) => {
                    return item == '颅内动脉瘤';
                  }) != -1
                  ? 'dark'
                  : 'plain': 'plain'
              "
            >
              颅内动脉瘤
            </el-tag>
            <el-tag
              v-if="scope.row.recordVoList && scope.row.recordVoList.length"
              :effect="
                scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  }) ? scope.row.recordVoList
                  .find((item) => {
                    return item.code == 'ZLLX-TYPE';
                  })
                  .answer.split(',')
                  .findIndex((item) => {
                    return item == 'CES/CAS';
                  }) != -1
                  ? 'dark'
                  : 'plain': 'plain'
              "
            >
              CES/CAS
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="leaveTime" label="出院时间" width="220">
        </el-table-column>
        <el-table-column prop="departmentName" label="所属科室" width="100">
        </el-table-column>
        <el-table-column prop="source" label="数据来源" width="100">
        </el-table-column>
        <el-table-column prop="filingUnit" label="建档单位" width="100">
        </el-table-column>
        <el-table-column prop="filingName" label="建档人" width="100">
        </el-table-column>
        <el-table-column prop="filingTime" label="建档时间" width="100">
        </el-table-column>
        <el-table-column prop="examineName" label="审核人" width="100">
        </el-table-column>
        <el-table-column prop="examineTime" label="审核时间" width="100">
        </el-table-column>
        <el-table-column prop="caseStatus" label="档案状态" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.caseStatus == '0'">未提交</div>
            <div v-else-if="scope.row.caseStatus == '1'">已提交</div>
            <div v-else-if="scope.row.caseStatus == '2'">已审核</div>
            <div v-else-if="scope.row.caseStatus == '3'">已驳回</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="160">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >详情</el-button
            >
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleStatus(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >重新审核</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="addForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="类别" prop="type">
              <el-select
                style="width: 100%"
                v-model="addForm.type"
                placeholder="请选择类别"
              >
                <el-option
                  v-for="item in dict.type.strategy_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="危险分级" prop="riskLevel">
              <el-select
                style="width: 100%"
                v-model="addForm.riskLevel"
                placeholder="请选择危险分级"
              >
                <el-option
                  v-for="item in dict.type.strategy_danger_level"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="随访轮次" prop="roundList">
              <el-checkbox-group v-model="addForm.roundList">
                <el-checkbox label="3">3个月</el-checkbox>
                <el-checkbox label="6">6个月</el-checkbox>
                <el-checkbox label="9">9个月</el-checkbox>
                <el-checkbox label="12">12个月</el-checkbox>
                <el-checkbox label="24">24个月</el-checkbox>
                <el-checkbox label="长期">长期</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="变量类别" prop="required">
              <el-radio-group v-model="addForm.required">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="策略内容" prop="content">
              <el-input
                style="width: 100%"
                type="textarea"
                :rows="6"
                placeholder="请输入内容"
                v-model="addForm.content"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertFlupInfo,
  updateFlupInfo,
  deleteFlupInfo,
  exportData,
} from "@/api/followUp/archives";
import {
  listProject,
} from "@/api/data/topic";
import { queryTagList } from "@/api/quality/tagging";
export default {
  dicts: [
    "strategy_type",
    "strategy_danger_level",
    "data_source",
    "sys_user_sex",
  ],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          idCard: null,
          name: null,
          projectId: null,
          hospitalizationNumber: null,
          doctorName: null,
          fileNum: null,
          caseStatus: null,
          departmentId: null,
          source: null,
          reportStatus: null,
          treatmentTypes: null,
          operationWay: null,
          operationName: [],
          leaveTime: null,
        },
      },
      tagList: [],
      dataList: [],
      total: 0,
      title: "",
      addDialog: false,
      addForm: {
        type: "",
        riskLevel: "",
        roundList: [],
        source: "",
        content: "",
      },
      rules: {
        text: [{ required: true, message: "请输入变量名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入变量CODE", trigger: "blur" }],
      },
      projectList: [],
    };
  },
  created() {
    this.getList();
    this.getProjectList();
  },
  methods: {
    // 获取数据列表
    getProjectList() {
      const param = {
        pageNum: 1,
        param: {
          name: "",
        },
      };
      console.log("param: ", param);
      listProject(param).then((response) => {
        this.projectList = [ ...response.data.list ];
        console.log('this.projectList: ', this.projectList);
      });
    },
    judge(type) {
      if (
        this.queryParams.param.treatmentTypes &&
        this.queryParams.param.treatmentTypes.length
      ) {
        if (type == "0" || type == "1") {
          if (
            this.queryParams.param.treatmentTypes.findIndex((item) => {
              return item == "AIS静脉溶栓";
            }) != -1
          ) {
            return false;
          } else {
            return true;
          }
        }
        if (type == "2" || type == "1") {
          if (
            this.queryParams.param.treatmentTypes.findIndex((item) => {
              return item == "AIS介入再通";
            }) != -1
          ) {
            return false;
          } else {
            return true;
          }
        }
        if (
          type == "3" ||
          type == "4" ||
          type == "5" ||
          type == "6" ||
          type == "7"
        ) {
          if (
            this.queryParams.param.treatmentTypes.findIndex((item) => {
              return item == "脑出血";
            }) != -1
          ) {
            return false;
          } else {
            return true;
          }
        }
        if (type == "8" || type == "9" || type == "6") {
          if (
            this.queryParams.param.treatmentTypes.findIndex((item) => {
              return item == "颅内动脉瘤";
            }) != -1
          ) {
            return false;
          } else {
            return true;
          }
        }
        if (type == "10" || type == "11" || type == "12" || type == "6") {
          if (
            this.queryParams.param.treatmentTypes.findIndex((item) => {
              return item == "CES/CAS";
            }) != -1
          ) {
            return false;
          } else {
            return true;
          }
        }
      } else {
        return true;
      }
    },
    // 导出数据
    downloadData() {
      const that = this;
      this.$modal
        .confirm("是否确认导出当前查询条件下的数据？")
        .then(function () {
          // 导出
          const params = that.queryParams;
          return that.download(
            exportData,
            { ...params.param },
            `异常数据导出.xlsx`
          );
        })
        .then((res) => {
          that.$message.success("导出成功");
        })
        .catch(() => {});
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const params = { ...this.queryParams };
      if (params.param.treatmentTypes) {
        params.param.treatmentType = params.param.treatmentTypes.toString();
      } else {
        params.param.treatmentType = "";
      }
      if (params.param.operationName.length) {
        params.param.operationName = params.param.join();
      } else {
        params.param.operationName = "";
      }
      if (params.param.leaveTime) {
        params.param.leaveTimeStart = params.param.leaveTime[0];
        params.param.leaveTimeEnd = params.param.leaveTime[1];
      } else {
        params.param.leaveTimeStart = null;
        params.param.leaveTimeEnd = null;
      }
      queryTagList(params).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增变量
    handleAdd() {
      this.title = "新增随访策略";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    async submitForm() {
      const param = { ...this.addForm };
      if (param.roundList.length) {
        param.source = param.roundList.join(",");
      } else {
        param.source = "";
      }
      if (param.id) {
        // 修改
        await updateFlupInfo(param).then((res) => {
          this.$message.success("修改成功");
          this.cancel();
          this.handleQuery();
        });
      } else {
        // 新增
        await insertFlupInfo(param).then((res) => {
          this.$message.success("新增成功");
          this.cancel();
          this.handleQuery();
        });
      }
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        type: "",
        riskLevel: "",
        roundList: [],
        source: "",
        content: "",
      };
      this.addDialog = false;
    },
    // 修改变量
    handleEdit(row) {
      this.$message.warning("查看详情");
      const id = row.id;
      // const type = this.queryParams.param.type;
      this.$router.push({
        path: "/data/archivesDetails",
        query: {
          id,
          // type,
          type: "1",
        },
      });
    },
    // 删除策略
    handleStatus(row) {
      this.$message.warning("重新审核");
    },
  },
};
</script>
<style lang="scss">
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.row-1 {
  width: 20%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.row-3 {
  width: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
